<template>
  <div class="app-container">
    <!-- 添加或修改客户回款对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="650px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="编号" prop="no">
          <el-input v-model="form.no" placeholder="请输入编号" />
        </el-form-item>
        <div v-if='this.form.id==null'>
        <el-form-item label="客户名称" prop="customId">
          <el-select v-model="form.customId" filterable  placeholder="请选择客户" @change="handleChange">
            <el-option
              v-for="custom in customList"
              :key="custom.id"
              :label="custom.name"
              :value="custom.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="客户编号" prop="customNo">
          <el-input v-model="form.customNo" placeholder="请输入客户编号" />
        </el-form-item>
        </div>
        <div v-if='this.form.id!=null'>
          <el-form-item label="客户名称" prop="customName">
            <el-input v-model="form.customName" readonly />
          </el-form-item>

          <el-form-item label="客户编号" prop="customNo">
            <el-input v-model="form.customNo" readonly />
          </el-form-item>
        </div>
        <el-form-item label="业务员" prop="userName">
          <el-select v-model="form.userId" filterable  placeholder="请选择业务员" @change="handleUserChange">
            <el-option
              v-for="user in userList"
              :key="user.userId"
              :label="user.userName"
              :value="user.userId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="业务类型" prop="type">
          <el-input v-model="form.type" placeholder="请输入业务类型" />
        </el-form-item>
        <el-form-item label="回款金额" prop="backMoney">
          <el-input-number v-model="form.backMoney" controls-position="right"  :min="0"  @input='getMoney' /><span style='margin-left: 6px'>元</span>
        </el-form-item>
        <el-form-item label="回款时间" prop="backTime">
          <el-date-picker clearable size="small"
            v-model="form.backTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择回款时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="佣金比例" prop="rate">
          <el-input-number  type='number' v-model="form.rate" placeholder="请输入佣金比例"  @input='getMoney'/><span style='margin-left: 6px'>%</span>
        </el-form-item>
        <el-form-item label="佣金金额" prop="rateMoney">
          <el-input-number v-model="form.rateMoney" controls-position="right"  :min="0"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getBack, addBack, updateBack } from "@/api/custom/back";
import { listAllCustom } from '@/api/custom/custom'
import { listUser } from '@/api/system/user'

export default {
  name: "BackModal",
  data() {
    return {
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      customList:[],
      userList:[],
      // 表单校验
      rules: {
        customId: [
          { required: true, message: "请选择客戶！", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getCustomList();
    this.getUserList();
  },
  methods: {
    getCustomList(){
      listAllCustom({status:2}).then(response => {
        this.customList = response.data;
      });
    },
    getUserList(){
      listUser({}).then(response => {
          this.userList = response.rows
        }
      )
    },
    handleChange(value) {
      console.log(value);
      this.selectedItem = this.customList.find(item => item.id === value) || null;
      this.form.customNo = this.selectedItem.no;
      this.form.customName = this.selectedItem.name;
    },
    handleUserChange(value){
      this.item = this.userList.find(item => item.userId === value) || null;
      this.form.userName = this.item.userName;
    },
    getMoney(){
      this.form.rateMoney = (this.form.rate *this.form.backMoney)/100;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        no: null,
        customId: null,
        customNo: null,
        customName: null,
        type: null,
        userName: null,
        backMoney: null,
        backTime: null,
        rate: null,
        rateMoney: null,
        createBy: null,
        createTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加客户回款";
    },
    /** 修改按钮操作 */
    handleUpdate(id) {
      this.reset();
      getBack(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改客户回款";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateBack(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.$emit('refresh')
            });
          } else {
            addBack(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.$emit('refresh')
            });
          }
        }
      });
    },
  }
};
</script>
